<template>
  <div>
    <el-switch
      v-model="cg"
      active-text="开启数据切换"
      :active-value="true"
      inactive-text="关闭数据切换"
      :inactive-value="false"
      @change="loop"
    >
    </el-switch>
    <!-- 准备绘制 -->
    <div ref="box" class="box">1111</div>
  </div>
</template>

<script>
// 导入所有图表
// import * as echarts from 'echarts'
// 按需引入
// import * as echarts from 'echarts/core'
// import { TooltipComponent } from 'echarts/components'
// import { RadarChart } from 'echarts/charts'
// import { CanvasRenderer } from 'echarts/renderers'

// echarts.use([TooltipComponent, RadarChart, CanvasRenderer])

export default {
  data () {
    return {
      cg: false,
      opt: {
        tooltip: {},
        radar: {
          axisName: {
            // textStyle: {
            color: '#fff',
            backgroundColor: '#999',
            borderRadius: 3,
            padding: [3, 5]
            // }
          },
          indicator: [
            { name: '工作效率', max: 100 },
            { name: '考勤', max: 100 },
            { name: '积极性', max: 100 },
            { name: '帮助同事', max: 100 },
            { name: '自主学习', max: 100 },
            { name: '正确率', max: 100 }
          ]
        },
        series: [
          {
            type: 'radar',
            data: [

            ]
          }
        ]
      }
    }
  },
  mounted () {
    this.initChart()
  },
  methods: {
    initChart () {
      //
      this.myChart = this.echarts.init(this.$refs.box)
      this.opt.series[0].data = [
        {
          value: [10, 1, 100, 5, 100, 0],
          name: '张三'
        },
        {
          value: [50, 50, 50, 50, 50, 10],
          name: '李四'
        }
      ]

      this.myChart.setOption(this.opt)
    },
    // 切换
    loop (f) {
      console.log(f)
      if (this.st) {
        clearInterval(this.st)
        this.st = null
        return
      }
      this.st = setInterval(this.change, 1000)
    },
    change () {
      const data = []
      for (let index = 0; index < 3; index++) {
        const per = { value: [], name: `李${index}` }
        for (let j = 0; j < 6; j++) {
          per.value.push(Math.floor(Math.random() * 100 + 1))
        }
        data.push(per)
      }
      // 绘制图表
      this.opt.series[0].data = data
      this.myChart.setOption(this.opt)
    }

  }

}
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 400px;
}
</style>
